//
//  YWMaterialDetailTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/8/29.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWMaterialDetailTableViewCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
//#import "UILabel+AttributedText.h"
#import "UIImageView+WebCache.h"
#import "YWCommonTool.h"

#import "YWMaterialApply.h"
#import "YWMaterialStockOut.h"
#import "YWMaterialReturn.h"
#import "YWHandler.h"
#import "YYLabel.h"

@interface YWMaterialDetailTableViewCell()
/** 头像 */
@property (nonatomic, strong) UIImageView *headImageView;
/** 申请人名称 */
@property (nonatomic, strong) YYLabel *applyNameLabel;
/**  申请时间 */
@property (nonatomic, strong) YYLabel *applyDateLabel;
/** 物料状态 */
@property (nonatomic, strong) YYLabel *MaterialStateTypeLabel;
/**  申请单号 */
@property (nonatomic, strong) YYLabel *applyNumbersLabel;
/**  接收人 */
@property (nonatomic, strong) YYLabel *receivedManNameLabel;
/**  抄送人 */
@property (nonatomic, strong) YYLabel * copiedManLabel;
/** 用途 */
@property (nonatomic, strong) YYLabel * noteLabel;
@end

@implementation YWMaterialDetailTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setUpSubviews{
    /* 头像 */
    
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.mas_equalTo(self.contentView).offset(8);
        make.width.and.height.mas_equalTo(@46);
    }];
    /* 申请人 */
    [self.applyNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_headImageView.mas_centerY).offset(-2);
        make.left.mas_equalTo(_headImageView.mas_right).offset(6);
    }];
    /* 申请时间 */
    [self.applyDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_applyNameLabel);
        make.top.mas_equalTo(_applyNameLabel.mas_bottom).offset(4);
    }];
    
    [self.MaterialStateTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.baseline.mas_equalTo(_applyNameLabel);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    /* 申请单号 */
    [self.applyNumbersLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headImageView.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    /* 接收人 */
    [self.receivedManNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_applyNumbersLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    /* 抄送人 */
    [self.copiedManLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_receivedManNameLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    /* 用途 */
    [self.noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_copiedManLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
        make.bottom.mas_equalTo(self.contentView).offset(-6);
    }];

}

//- (void)layoutSubviews{
//    [super layoutSubviews];
//    
//}


- (void)setApply:(YWMaterialApplyViewModel *)apply
{
    _apply = apply;
    //    头像
    NSString* imageUrl = [YWCommonTool pictureDownloadUrl:_apply.creator.headIcon];
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"profile_headimage"]];
    _applyNameLabel.text = _apply.creator.userName?:@"未知用户";
    _applyDateLabel.text = [YWCommonTool showTimeWithNormalFormatter:_apply.applyTime /1000];
    _applyNumbersLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"申请单号: " firstColor:[UIColor customTextGrayBlackColor] secondText:_apply.applySequence?:@"无"  secondColor:[UIColor customYellowColor] font:kContentFont];
    _MaterialStateTypeLabel.text =_apply.status == YWMaterialDataStatusCancel?@"已撤回":@"未领用";
    NSMutableString * receipientString = [NSMutableString string];
    [_apply.receivers enumerateObjectsUsingBlock:^(YWHandler *  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx!=0) {
            [receipientString appendString:@","];
        }
        [receipientString appendString:[NSString stringWithFormat:@"%@",handler.userName]];
    }];
    
    _receivedManNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"接收人: " firstColor:[UIColor customTextGrayBlackColor] secondText:receipientString  secondColor:[UIColor blackColor] font:kContentFont];
    NSMutableString * copierString = [NSMutableString string];
    [_apply.observers enumerateObjectsUsingBlock:^(YWHandler *  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx!=0) {
            [copierString appendString:@","];
        }
        [copierString appendString:[NSString stringWithFormat:@"%@",handler.userName?:@"无"]];
    }];
    if (copierString.length>0) {
        _copiedManLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"抄送人: " firstColor:[UIColor customTextGrayBlackColor] secondText:copierString  secondColor:[UIColor blackColor] font:kContentFont];
    }else{
        _copiedManLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"抄送人: " firstColor:[UIColor customTextGrayBlackColor] secondText:@"无"  secondColor:[UIColor blackColor] font:kContentFont];
    }
    
    if (_apply.note.length>0) {
        _noteLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"用途: " firstColor:[UIColor customTextGrayBlackColor] secondText:_apply.note  secondColor:[UIColor blackColor] font:kContentFont];
    }else{
        _noteLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"用途: " firstColor:[UIColor customTextGrayBlackColor] secondText:@"无"  secondColor:[UIColor blackColor] font:kContentFont];
    }
    
    
    self.noteLabel.preferredMaxLayoutWidth = self.noteLabel.frame.size.width;
    self.receivedManNameLabel.preferredMaxLayoutWidth = self.receivedManNameLabel.frame.size.width;
    self.copiedManLabel.preferredMaxLayoutWidth = self.copiedManLabel.frame.size.width;
}

- (void)setReceipient:(YWMaterialStockOut *)receipient
{
    _receipient = receipient;
    NSString* imageUrl = [YWCommonTool pictureDownloadUrl:_receipient.receiver.headIcon];
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"profile_headimage"]];
    _MaterialStateTypeLabel.text = _receipient.stockOutStatus==0?@"未领用": @"已领用";
    _applyNameLabel.text = _receipient.receiver.userName?:@"未知用户";
    _applyDateLabel.text = [YWCommonTool showTimeWithNormalFormatter:_receipient.stockOutTime /1000];
    _applyNumbersLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"领用单号: " firstColor:[UIColor customTextGrayBlackColor] secondText:_receipient.stockOutSequence secondColor:[UIColor customYellowColor] font:kContentFont];
    //仓管
    _receivedManNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"发放人: " firstColor:[UIColor customTextGrayBlackColor] secondText:_receipient.creator.userName?:@"未知用户" secondColor:[UIColor blackColor] font:kContentFont];
    
    self.noteLabel.preferredMaxLayoutWidth = self.noteLabel.frame.size.width;
    self.receivedManNameLabel.preferredMaxLayoutWidth = self.receivedManNameLabel.frame.size.width;
    self.copiedManLabel.preferredMaxLayoutWidth = self.copiedManLabel.frame.size.width;
}

- (void)setReturnBack:(YWMaterialReturn *)returnBack
{
    _returnBack = returnBack;
    NSString* imageUrl = [YWCommonTool pictureDownloadUrl:_returnBack.returner.headIcon];
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"profile_headimage"]];
    _applyNameLabel.text = _returnBack.returner.userName?:@"未知用户";
    _applyDateLabel.text = [YWCommonTool showTimeWithNormalFormatter:_returnBack.returnTime /1000];
    _MaterialStateTypeLabel.text = _returnBack.returnStatus == 0?@"未归还" :@"已归还";
    _applyNumbersLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"归还单号: " firstColor:[UIColor customTextGrayBlackColor] secondText:_returnBack.returnSequence secondColor:[UIColor customYellowColor] font:kContentFont];
    //仓管
    _receivedManNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"接收人: " firstColor:[UIColor customTextGrayBlackColor] secondText:_returnBack.creator.userName?:@"未知用户" secondColor:[UIColor blackColor] font:kContentFont];
    
    self.noteLabel.preferredMaxLayoutWidth = self.noteLabel.frame.size.width;
    self.receivedManNameLabel.preferredMaxLayoutWidth = self.receivedManNameLabel.frame.size.width;
    self.copiedManLabel.preferredMaxLayoutWidth = self.copiedManLabel.frame.size.width;
}

- (UIImageView *)headImageView{
    if (!_headImageView) {
        UIImageView *imageView = [UIImageView new];
        imageView.clipsToBounds = YES;
        imageView.layer.cornerRadius = 46/2;
        _headImageView = imageView;
        [self.contentView addSubview:_headImageView];
    }
    return _headImageView;
}

- (YYLabel *)applyNameLabel{
    if (!_applyNameLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor blackColor];
        _applyNameLabel = label;
        [self.contentView addSubview:_applyNameLabel];
    }
    return _applyNameLabel;
}

- (YYLabel *)applyDateLabel{
    if (!_applyDateLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        _applyDateLabel = label;
        [self.contentView addSubview:_applyDateLabel];
    }
    return _applyDateLabel;
}

- (YYLabel *)MaterialStateTypeLabel{
    if (!_MaterialStateTypeLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor blackColor];
        _MaterialStateTypeLabel = label;
        [self.contentView addSubview:_MaterialStateTypeLabel];
    }
    return _MaterialStateTypeLabel;
}

- (YYLabel *)applyNumbersLabel{
    if (!_applyNumbersLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        _applyNumbersLabel = label;
        [self.contentView addSubview:_applyNumbersLabel];
    }
    return _applyNumbersLabel;
}

- (YYLabel *)receivedManNameLabel{
    if (!_receivedManNameLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _receivedManNameLabel = label;
        [self.contentView addSubview:_receivedManNameLabel];
    }
    return _receivedManNameLabel;
}

- (YYLabel *)copiedManLabel{
    if (!_copiedManLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _copiedManLabel = label;
        [self.contentView addSubview:_copiedManLabel];
    }
    return _copiedManLabel;
}

- (YYLabel *)noteLabel{
    if (!_noteLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _noteLabel = label;
        [self.contentView addSubview:_noteLabel];
    }
    return _noteLabel;
}

@end
